package com.ozo.boot.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ozo.boot.common.base.model.BaseBatchDTO;
import com.ozo.boot.common.base.model.BasePage;
import com.ozo.boot.common.base.model.PageResult;
import com.ozo.boot.common.database.util.SortUtils;
import com.ozo.boot.system.entity.SysNotice;
import com.ozo.boot.system.mapper.SysNoticeMapper;
import com.ozo.boot.system.pojo.dto.SysNoticeQueryDTO;
import com.ozo.boot.system.pojo.dto.SysNoticeSaveDTO;
import com.ozo.boot.system.pojo.vo.SysNoticeVO;
import com.ozo.boot.system.service.ISysNoticeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公告 服务实现类
 *
 * @author qiangesoft
 * @date 2024-11-19 21:58:19
 * @version 1.0
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SysNoticeSaveDTO saveDTO) {
        SysNotice sysNotice = BeanUtil.copyProperties(saveDTO, SysNotice.class);
        baseMapper.insert(sysNotice);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(BaseBatchDTO<Long> batchDTO) {
        baseMapper.deleteBatchIds(batchDTO.getIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysNoticeSaveDTO saveDTO) {
        SysNotice sysNotice = BeanUtil.copyProperties(saveDTO, SysNotice.class);
        baseMapper.updateById(sysNotice);
    }

    @Override
    public PageResult<SysNoticeVO> page(BasePage basePage, SysNoticeQueryDTO queryDTO) {
        Integer pageNum = basePage.getPageNum();
        Integer pageSize = basePage.getPageSize();

        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper<>();
        // 排序
        String order = basePage.getOrder();
        if (StrUtil.isBlank(order)) {
            queryWrapper.lambda().orderByDesc(SysNotice::getId);
        } else {
            SortUtils.setSortCondition(queryWrapper, basePage.getProp(), order);
        }
        // 条件
        queryWrapper.lambda()
                .like(StrUtil.isNotBlank(queryDTO.getTitle()), SysNotice::getTitle, queryDTO.getTitle())
                .eq(StrUtil.isNotBlank(queryDTO.getNoticeType()), SysNotice::getNoticeType, queryDTO.getNoticeType());
        Page<SysNotice> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);

        PageResult<SysNoticeVO> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setPage(page.getPages());
        pageResult.setTotal(page.getTotal());
        List<SysNotice> recordList = page.getRecords();
        List<SysNoticeVO> recordVOList = BeanUtil.copyToList(recordList, SysNoticeVO.class);
        pageResult.setResults(recordVOList);
        return pageResult;
    }

}
